package com.example.zy.car.view;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zy.car.R;
import com.example.zy.car.base.BaseActivityWithTitleBar;
import com.example.zy.car.entity.ContentState;
import com.example.zy.car.entity.SelectState;
import com.example.zy.car.entity.TextEntity;
import com.example.zy.car.view.camera.CameraActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class EvaluateSelectActivity extends BaseActivityWithTitleBar implements ViewPager.OnPageChangeListener, AdapterView.OnItemClickListener {

    @Bind(R.id.vp_evaluate_select)
    ViewPager mVpEvaluateSelect;
    @Bind(R.id.gv_evaluate_select)
    GridView mGvEvaluateSelect;

    private int[] ids = {R.drawable.p1, R.drawable.p2, R.drawable.p3};
    private Drawable[] imgs = new Drawable[ids.length];
    private MyVPAdapter mMyVPAdapter;

    private List<TextEntity> mEntityList = new ArrayList<>();
    private MyGVAdapter mMyGVAdapter;
    private String[] mStrings = {"正面照片", "前保险杠", "前机盖"};

    private int mVpPosition = 0;
    private static final int REQUESTCODE=100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_evaluate_select);
        ButterKnife.bind(this);
        this.setCenterTitleTv("正前照");
        this.setLeftTitleTv("返回", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(R.anim.extre_in,R.anim.extre_out);
            }
        });
        this.setRightTitleTv("下一项",null);
//        mVpEvaluateSelect = (ViewPager) this.findViewById(R.id.vp_evaluate_select);
//        mGvEvaluateSelect = (GridView) this.findViewById(R.id.gv_evaluate_select);

        for (int i = 0; i < ids.length; i++) {
            imgs[i] = this.getResources().getDrawable(ids[i]);
            TextEntity textEntity = new TextEntity();
            textEntity.setString(mStrings[i]);
            if (i == 0) {
                textEntity.setSelectState(SelectState.select);
            } else {
                textEntity.setSelectState(SelectState.nonselect);
            }
            textEntity.setContentState(ContentState.oldContent);
            mEntityList.add(textEntity);
        }

        mMyVPAdapter = new MyVPAdapter();
        mVpEvaluateSelect.setAdapter(mMyVPAdapter);
        mVpEvaluateSelect.setCurrentItem(0);
        mVpEvaluateSelect.setOffscreenPageLimit(2);
        mVpEvaluateSelect.addOnPageChangeListener(this);
        mMyGVAdapter = new MyGVAdapter();
        mGvEvaluateSelect.setAdapter(mMyGVAdapter);
        mGvEvaluateSelect.setOnItemClickListener(this);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mVpEvaluateSelect.setCurrentItem(position);
        for (int i = 0; i < mEntityList.size(); i++) {
            if (position == i) {
                mEntityList.get(i).setSelectState(SelectState.select);
            } else {
                mEntityList.get(i).setSelectState(SelectState.nonselect);
            }
        }
        mMyGVAdapter.notifyDataSetChanged();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        onItemClick(null, null, position, 0);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    //ViewPager的适配器
    class MyVPAdapter extends PagerAdapter implements View.OnClickListener {

        //获取数目
        @Override
        public int getCount() {
            return ids.length;
        }

        //初始化每个条目
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView view = (ImageView) View.inflate(EvaluateSelectActivity.this, R.layout.vp_item, null);
            view.setImageDrawable(imgs[position]);
            view.setTag(position);
            view.setOnClickListener(this);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void onClick(View v) {
            Toast.makeText(EvaluateSelectActivity.this, "点到了", Toast.LENGTH_SHORT).show();
            //打开相机
            Intent intent = new Intent(EvaluateSelectActivity.this, CameraActivity.class);
            mVpPosition = (int) v.getTag();
            startActivityForResult(intent, REQUESTCODE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 1) {
            if (requestCode == REQUESTCODE) {
                Bundle bundle = data.getBundleExtra("bundle");
                Drawable drawable = Drawable.createFromPath(bundle.getString("filePath"));
                System.out.println(bundle.getString("filePath")+"------------------------------->");
                imgs[mVpPosition].setCallback(null);//回收
                imgs[mVpPosition] = drawable;
                ImageView imageView = (ImageView) mVpEvaluateSelect.findViewWithTag(mVpPosition);
                imageView.setImageDrawable(drawable);
                mEntityList.get(mVpPosition).setContentState(ContentState.newContent);
            }
        }
    }

    //GrideView的适配器
    class MyGVAdapter extends BaseAdapter {
        LayoutInflater mLayoutInflater;

        public MyGVAdapter() {
            mLayoutInflater = EvaluateSelectActivity.this.getLayoutInflater();
        }

        @Override
        public int getCount() {
            return mEntityList.size();
        }

        @Override
        public Object getItem(int position) {
            return mEntityList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = mLayoutInflater.inflate(R.layout.gv_item, parent, false);
                ViewHolder viewHolder = new ViewHolder();
                viewHolder.mTextView = (TextView) convertView.findViewById(R.id.tv_gv_item);
                convertView.setTag(viewHolder);
            }
            initializeViews((TextEntity) getItem(position), (ViewHolder) convertView.getTag());
            return convertView;
        }

        private void initializeViews(TextEntity entity, ViewHolder holder) {
            holder.mTextView.setText(entity.getString());
            if (entity.getContentState() == ContentState.oldContent) {//旧的
                if (entity.getSelectState() == SelectState.select) {//选中
                    holder.mTextView.setBackgroundResource(R.color.selector);
                } else {//未选中
                    holder.mTextView.setBackgroundResource(R.color.oldcontent);
                }
            } else {//新的
                if (entity.getSelectState() == SelectState.select) {//选中
                    holder.mTextView.setBackgroundResource(R.color.selector);
                } else {//未选中
                    holder.mTextView.setBackgroundResource(R.color.newcontent);
                }
            }
        }

        class ViewHolder {
            TextView mTextView;
        }
    }


}
